package data;

import java.io.*;
import java.util.ArrayList;

import model.MainModel;
import model.trajectory.TargetModel;



public class TrajectoryLoader 
{
	public static TargetModel readTrajectory(MainModel model, String file)
	{
		/*
		TrajectoryModel traj = new TrajectoryModel();
		TargetModel target = new TargetModel(0,"test", traj);
		//lecture du fichier texte	
		try{
			InputStream inputStream = new FileInputStream(file); 
			InputStreamReader streamReader =new InputStreamReader(inputStream);
			BufferedReader br=new BufferedReader(streamReader);
			
			String ligne;
			
			while ( (ligne=br.readLine()) != null )
			{
				String[] position = ligne.split(";");
				
				float x = Float.parseFloat(position[0].replace(";", ""));
				float y = Float.parseFloat(position[1].replace(";", ""));
				long date = Long.parseLong(position[3].replace(";", ""));
				int id = Integer.parseInt(position[2].replace(";", ""));
				
				new TrajPosition(x, y, date, model.getRoomById(id)); 
				
				//traj.Add(pos);
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		*/
		return null;
	}
	
	public static void writeTrajectory(String text)
	{
		String file = "trajectory" + "_" + TrajectoryLoader.count("trajectory", TrajectoryLoader.findFiles("./src/data/trajectories/"));
		file =  "./src/data/trajectories/"+file+".dat";
		
		try
		{
			FileWriter fw = new FileWriter(file, true);
			BufferedWriter output = new BufferedWriter(fw);
			output.write(text);
			output.flush();
			output.close();
		}
		catch(IOException ioe)
		{
			System.out.print("Erreur : ");
			ioe.printStackTrace();
		}
	}
	
	public static File[] findFiles(String directoryPath) 
	{
		File directory = new File(directoryPath);
		
		if(directory.exists() && directory.isDirectory())
		{
			return directory.listFiles();
		}
		return new File[0];
	}
	
	public static ArrayList<TargetModel> LoadTrajectories(String path, MainModel model)
	{
		ArrayList<TargetModel> result = new ArrayList<TargetModel>();
		
		File[] files = TrajectoryLoader.findFiles(path);
		for(int i = 0; i < files.length; i++)
		{
			result.add(readTrajectory(model, path + files[i].getName()));
		}
		
		return result;
	}
	
	public static int count(String beginStr, File[] files)
	{
		int count = 0;
		for(int i = 0; i < files.length; i++)
		{
			if(files[i].getName().contains(beginStr))
			{
				count++;
			}
		}
		return count;
	}
}
